// packageI/goodLimit/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowTimes: new Date(),
    list: [],
    isLoadMore: true,
    page: 1,
    last_page: 1,

    pageOptions: {},
    timeData: [],
    currentTime: 0,
    _arr:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.currentTime = parseInt(new Date().getTime() / 1000);
    this.getData();
  },

  onChange(e) {
    this.data._arr[e.target.dataset.id] = {
      ...e.detail,
      id:e.target.dataset.id
    }
    this.setData({
      timeData: this.data._arr,
    });
  },

  getData() {
    let urlStr = app.getNetAddresss("plugin.goods-limit-set.frontend.controllers.index.index");
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            currentTime: parseInt(new Date().getTime() / 1000),
            list: res.data.list.data,
            total_page: res.data.list.last_page,
            isLoadMore: true,
            pageOptions: res.data.set
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss("plugin.goods-limit-set.frontend.controllers.index.index");
      urlStr += `&page=${this.data.page}`;
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let nextPageData = res.data.list.data;
            let list = this.data.list.concat(nextPageData);
            this.setData({
              list
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

  gotoDetail(e) {
    let id = e.currentTarget.dataset.item.id;
    if (e.currentTarget.dataset.item.plugin_id == 78) {
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + id + '&md=1'
      });
    } else {
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + id,
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})